import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-04-10
 * Time: 11:14
 */
class Money implements Cloneable{
    double money = 15.5;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    int id = 1234;
    Money m = new Money();
    //此处重写的是 Onject 方法中的 clone 方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //调用Object中的clone方法，先将获得引用存入temp
        Student temp = (Student) super.clone();
        //通过temp.m 访问到的是拷贝出的新的对象中的 m 引用
        // this.m 访问到的是当前对象中的 m 引用
        temp.m = (Money)this.m.clone();
        return temp;
    }
}
public class Test05 {
    public static void main(String[] args) throws CloneNotSupportedException {
        //浅拷贝
        Student student1 = new Student();
        Student student2 ;
        //clone方法的返回类型是Object，而student2的类型是Student,此处需要向下转型
        student2 = (Student) student1.clone();
        System.out.println(student1.id + " " + student1.m.money);
        System.out.println(student2.id + " " + student2.m.money);
        System.out.println("=====================");
        student2.m.money = 99.9;
        System.out.println(student1.id + " " + student1.m.money);
        System.out.println(student2.id + " " + student2.m.money);
        //此处修改了student2 中的money的值，只有student2中的值改变了，student1中的money的值未改变
        // 说明此处的拷贝 将 money 引用的对象空间也重新拷贝了一份。
    }
}
